#include "entity_manager_test.h"

#include "space\base_compat.h"
#include "space\managers\entity_manager.h"
#include "space\engines\client.h"
#include "space\i_engine.h"

using namespace space;
using namespace space::managers;

// TODO: Add this macro to the final
#define SYNC_UPDATE_CYCLE(the_object, engine, delta) { \
	the_object->on_pre_update(engine, delta); \
	the_object->on_update(engine, delta); \
	the_object->on_post_update(engine, delta); \
}
// TODO: ADD THIS MACRO
#define RELEASE_MANAGER(x, engine) { \
	x->release(engine); \
	delete x; \
	x = NULL; \
}


void test_collection_managers(void) {
	space::i_engine_ptr client_ptr;
	entity_manager *manager;
	entity_ptr entity, entity_2;

	LOG("Initializing..");
	manager = new entity_manager();
	manager->initialize(client_ptr);

	// create an entity
	LOG("Creating an entity...");
	entity = manager->create("FAGGY");
	entity_2 = manager->create();
	LOG("Tagging entity...");
	manager->assign_tag("FUCKYOU_1", entity);
	manager->assign_tag("FUCKYOU_2", entity);
	manager->assign_tag("FUCKYOU_2", entity_2);

	LOG("Running a tick to process collection updates");
	SYNC_UPDATE_CYCLE(manager, client_ptr, 0);

	LOG("Giving (entity_2) a name: BLOWHOLE");
	entity_2->set_name("BLOWHOLE");

	LOG("Current Tag names from Test:");
	std::vector<std::string> tags;
	manager->tags(tags);

	std::vector<std::string>::iterator iter;
	for(iter = tags.begin(); iter != tags.end(); iter ++) {
		LOG("\t[%s]", iter->c_str());
	}

	LOG("Searching for entity by tag");
	std::vector<entity_ptr> by_tag;
	result res = manager->find_by_tag("FUCKYOU_1", by_tag);
	LOG("\tFUCKYOU_1 = vector.size()=%d", by_tag.size());
	res = manager->find_by_tag("FUCKYOU_2", by_tag);
	LOG("\tFUCKYOU_2 = vector.size()=%d", by_tag.size());

	LOG("Accessing master entity collection..");
	std::vector<entity_ptr> tempList = manager->entities().items();
	std::vector<entity_ptr>::iterator entity_v_iter;
	for(entity_v_iter = tempList.begin(); entity_v_iter != tempList.end(); entity_v_iter ++) {
		entity_ptr ptr = *entity_v_iter;
		LOG("\t[%s]", ptr->name().c_str());
	}


	LOG("Searching for entities by name (both pre and post assigned");
	entity_ptr  test_srch;
	test_srch = manager->find_by_name("FAGGY");
	LOG("FAGGY = %08x", test_srch.get());
	test_srch = manager->find_by_name("BLOWHOLE");
	LOG("BLOWHOLE = %08x", test_srch.get());

	LOG("Removing entity_2");
	manager->destroy(entity_2);

	LOG("Removing entity");
	manager->destroy(entity);

	LOG("Entities were destroyed, cycling another tick...");
	SYNC_UPDATE_CYCLE(manager, client_ptr, 0);

	LOG("Tick complete, re-searching tag and checking sizes...");
	LOG("Searching for entities by name (both pre and post assigned");
	test_srch = manager->find_by_name("FAGGY");
	LOG("FAGGY = %08x", test_srch.get());
	test_srch = manager->find_by_name("BLOWHOLE");
	LOG("BLOWHOLE = %08x", test_srch.get());

	LOG("Searching for entity by tag");
	res = manager->find_by_tag("FUCKYOU_1", by_tag);
	LOG("\tFUCKYOU_1 = vector.size()=%d", by_tag.size());
	res = manager->find_by_tag("FUCKYOU_2", by_tag);
	LOG("\tFUCKYOU_2 = vector.size()=%d", by_tag.size());


	LOG("Release manager");
	RELEASE_MANAGER(manager, client_ptr);

}